package com.iwhalecloud.bss.kite.cucc.web.controller.order;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.ProductModuleVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.service.even.GroupProdAttrEvent;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.core.LocalAcceptEngine;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.ztesoft.bss.common.util.SpringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <Description> 结算界面<br>
 *
 * @author zhu.zixin<br>
 * @version 1.0<br>
 * @taskId <br>
 */
@RestController
@RequestMapping("/cuccsettle")
@Api(tags = {
    "/settle - 订单预览页面"
})
public class LocalSettlementController {

    @Autowired
    private LocalAcceptEngine acceptEngine;

    @Autowired
    private ApplicationContext applicationContext;

    @ApiOperation(value = "预览场景实例", notes = "预览场景实例")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sceneInstId", value = "场景实例标识", required = true),
        @ApiImplicitParam(name = "offerId", value = "销售品标识", required = true),
        @ApiImplicitParam(name = "templateId", value = "模板Id", required = true)
    })
    @GetMapping
    public DataResult<SceneNewOfferSeneVO> previewSceneInst(@RequestParam String sceneInstId, @RequestParam String templateId, @RequestParam String offerId) {
        // 批量受理重提详情页面，渲染页面前增加当前场景实例id
        if (Objects.nonNull(SpringUtil.getBean(IQuerySceneInstService.class).getExtParameterValue(sceneInstId, "batchNodeId")) &&
            KiteStringUtils.isEmpty(KiteWebContext.getContext().getCurrentSceneInstId())) {
            KiteWebContext.addAndUpdateSceneInstId(sceneInstId);
        }
        SceneNewOfferSeneVO sceneVO = (SceneNewOfferSeneVO) acceptEngine.previewScene(sceneInstId, KiteWebContext.getContext().getCustId(), templateId, offerId);
        //发布属性分组处理事件
        List<ProductModuleVO> productModuleVOList = sceneVO.getSceneProductModuleVO().getProduct();
        GroupProdAttrEvent groupProdAttrEvent = new GroupProdAttrEvent(this, KiteWebContext.getContext().getCustId(), "");
        groupProdAttrEvent.setSceneInstId(sceneInstId);
        groupProdAttrEvent.setProductModuleVOList(productModuleVOList);
        applicationContext.publishEvent(groupProdAttrEvent);
        if (Objects.nonNull(SpringUtil.getBean(IQuerySceneInstService.class).getExtParameterValue(sceneInstId, "batchNodeId")) &&
            KiteStringUtils.isEmpty(sceneVO.getCustId())) {
            sceneVO.setCustId(KiteWebContext.getContext().getCustId());
        }
        return ErrorConsts.SUCCESS.getResult(sceneVO);
    }


}
